package com.clkj.zhonghui.controller.stored;

import com.clkj.zhonghui.pojo.common.Result;
import com.clkj.zhonghui.pojo.common.StatusCode;
import com.clkj.zhonghui.pojo.stored.Stored;
import com.clkj.zhonghui.pojo.stored.UserStoredDetail;
import com.clkj.zhonghui.service.stored.StoredService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author yawang
 * @create 2024/07/15 09:59:47
 **/
@RestController
@RequestMapping(value = "/stored")
@Api(value = "/stored", tags = {"储值金信息"})
@CrossOrigin
public class StoredController {

    @Autowired
    private StoredService storedService;

    @PostMapping("/listMain")
    @ApiOperation(value = "app--获取储值金首页")
    public Result listMain(Integer userId,Integer shopId){
        try {
            Map map = storedService.listMain(userId,shopId);
            return new Result(true, StatusCode.OK,"获取储值金首页成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"获取储值金首页失败");
        }
    }
    @PostMapping("/listDetail")
    @ApiOperation(value = "app--获取储值存储记录")
    public Result listDetail(Integer userId,
                             @ApiParam(name="flag",value="0为储值，1为消费")@RequestParam Integer flag){
        try {
            List<UserStoredDetail> details  = storedService.listDetail(userId,flag);
            return new Result(true, StatusCode.OK,"获取储值金首页成功",details);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"获取储值金首页失败");
        }
    }

    @PostMapping("/listShareDetail")
    @ApiOperation(value = "app--我的邀请返利详情")
    public Result listShareDetail(Integer userId){
        try {
            List<UserStoredDetail> details  = storedService.listShareDetail(userId);
            return new Result(true, StatusCode.OK,"获取储值金首页成功",details);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"获取储值金首页失败");
        }
    }

    @PostMapping("/userPay")
    @ApiOperation(value = "app/微信端用户储值")
    public Result userPay(Integer userId,Integer shopId,
                          @ApiParam(name="shareId",value="上级ID，没有传0")@RequestParam("shareId")Integer shareId,
                          @ApiParam(name="storedMoney",value="充值金额")@RequestParam("storedMoney")Double storedMoney,
                          @ApiParam(name="rebate",value="用户充值返利")@RequestParam("rebate")Double rebate,
                          @ApiParam(name="shareRebate",value="给上级返利")@RequestParam("shareRebate")Double shareRebate,
                          @ApiParam(name="flag",value="0app充值 1微信端充值")@RequestParam("flag")Integer flag,
                          @ApiParam(name="openId",value="APP充值传0，微信段充值传openId")@RequestParam("openId")String openId,
                          @ApiParam(name="ifDown",value="是否已经绑定了上级，0为否，1为是")@RequestParam("ifDown")Integer ifDown,
                          HttpServletRequest request){
        try {
           Map map   = storedService.userPay(userId,shopId,shareId,storedMoney,rebate,shareRebate,flag,openId,ifDown,request);
            return new Result(true, StatusCode.OK,"获取储值金首页成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"获取储值金首页失败");
        }
    }

    @PostMapping("/paySuccess")
    @ApiOperation(value = "app/微信端用户储值成功回调")
    public Result paySuccess(HttpServletRequest request, HttpServletResponse response){
        try {
            storedService.paySuccess(request,response);
            return new Result(true, StatusCode.OK,"获取储值金首页成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"获取储值金首页失败");
        }
    }

    @PostMapping("/listDrawMain")
    @ApiOperation(value = "app/获取抽奖首页")
    public Result listDrawMain(Integer shopId,Integer userId,
                               @ApiParam(name="flag",value = "1为抽奖金额")@RequestParam Integer flag){
        try {
            Map map = storedService.listDrawMain(shopId,userId,flag);
            return new Result(true, StatusCode.OK,"获取抽奖首页",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"获取抽奖首页失败");
        }
    }

    @PostMapping("/listActMain")
    @ApiOperation(value = "app/获取3650活动首页")
    public Result listActMain(Integer userId){
        try {
            Map map = storedService.listActMain(userId);
            return new Result(true, StatusCode.OK,"获取3650活动首页",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"获取抽奖首页失败");
        }
    }

    @PostMapping("/addActDetail")
    @ApiOperation(value = "app/3650活动完成每日任务")
    public Result addActDetail(Integer userId,
                               @ApiParam(name="dayNum",value="当月总天数")@RequestParam Integer dayNum){
        try {
            Map map = storedService.addActDetail(userId,dayNum);
            return new Result(true, StatusCode.OK,"3650活动完成每日任务",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"获取抽奖首页失败");
        }
    }
    @PostMapping("/addCoupon")
    @ApiOperation(value = "app/3650活动获取优惠券")
    public Result addCoupon(Integer userId){
        try {
            Map map = storedService.addCoupon(userId);
            return new Result(true, StatusCode.OK,"3650活动完成每日任务",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"获取抽奖首页失败");
        }
    }

    @ApiOperation(value = "app--获取我的专属海报")
    @PostMapping("/listUserImg")
    public Result listUserImg(Integer userId,String  picName,Integer shopId){
        try {
            Map map  = storedService.listUserImg(userId,picName,shopId);
            return new Result(true, StatusCode.OK,"查询成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }

    @PostMapping("/addStored")
    @ApiOperation(value = "后台---添加储值金")
    public Result addStored(Stored stored){
        try {
            storedService.addStored(stored);
            return new Result(true, StatusCode.OK,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"添加失败");
        }
    }
    @PostMapping("/listStored")
    @ApiOperation(value = "后台---获取储值金")
    public Result listStored(@ApiParam(name="flag",value="0为储值金 1为抽奖金额")@RequestParam Integer flag){
        try {
            List<Stored> storedList = storedService.listStored(flag);
            return new Result(true, StatusCode.OK,"查询成功",storedList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }

    @PostMapping("/deleteStored")
    @ApiOperation(value = "后台---删除储值金")
    public Result deleteStored(String ids){
        try {
             storedService.deleteStored(ids);
            return new Result(true, StatusCode.OK,"查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }

    @ApiOperation(value = "后台--后台用户充值列表")
    @PostMapping("/listAllUserStored")
    public Result listAllUserStored(Integer page,Integer size,Integer userId){
        try {
            PageInfo storedList = storedService.listAllUserStored(page,size,userId);
            return new Result(true, StatusCode.OK,"查询成功",storedList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }
    @ApiOperation(value = "后台--后台用户充值消费详情")
    @PostMapping("/listUserStoredDetail")
    public Result listUserStoredDetail(Integer userId){
        try {
            List<UserStoredDetail> storedList = storedService.listUserStoredDetail(userId);
            return new Result(true, StatusCode.OK,"查询成功",storedList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }


}
